<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/2 0002
 * Time: 上午 12:17
 */
namespace app\dlc\common\createHelper;
use app\dlc\controller\CommonController;
use think\Db;

class CreateController extends Common
{
    public static $path = 'application/dlc/controller/';
    public $table;
    public $edit = '_edit';
    public $list_string;
    public function create($table,$cover=true){
        $fileNname = $this->camelize($table,1);
        $fields = self::getTableInfo($table);
        $this->table = $table;
        if (!$fields) return;
        $this->phpStart();
        $this->space('namespace app\dlc\controller;
use think\Db;');
        $this->classNameStart("class ".$fileNname."Controller extends CommonController");
        $this->actionStart("public function index()");
        $this->addIndex($table,$fields);
        $this->actionEnd();
        if ($this->edit == '_edit'){
            $this->addEdit();
            $this->addDel();
        }else{
            $this->addRoleEdit();
            $this->addRoleDel();
        }

        $this->classNameEnd();        //在类结束之前，要加载一些方法
        $this->phpEnd();
        $this->save(self::$path.$fileNname.'Controller.php',$cover,1);
    }

// ---------------------------   index 方法开始  ------------------------------------------------------------------------
    public function addIndex($table,$fields){
        $lk = [];
        foreach ($fields as $key => $field) {
            if (strstr($field,'_id')) $lk[] = str_replace('_id','',$field);
        }
        $with = implode(',',$lk);
        //查找该表的所有数据
        $this->list_string .= '$list = [';
        $data = Db::name('form_list_item')->where(['table'=>$table])->order('type,sort asc')->select();
        $arr = [1=>'head_botton',2=>'head_search',3=>'list_botton',4=>'list_title'];
        foreach ($data as $key => $item) {
            $action = $arr[$item['type']];
            $this->$action($item);
        }
        $this->content('$this->loading();');
        $this->content('$map = $this->map();');
        $this->content('$data = model(CONTROLLER_NAME)->where($map)->order(\'id desc\')->page(self::$page)->select();');
        $this->content('$count = model(CONTROLLER_NAME)->where($map)->count();');
        $this->content('$this->getPage($count, self::$psize, \'App-loader\', $this->title, \'App-search\');');
        $this->content('$this->assign(\'data\',$data);');
        $this->list_string .= '\'sys\'=>"操作"];';
        $this->content($this->list_string);
//        $this->content('$list = [\'id\'=>\'ID\',\'name\'=>\'name\',\'ctime\'=>\'创建时间\',\'sys\'=>\'操作\'];');
        $this->content('$this->assign(\'list\',$list);');
        $this->content('echo $this->fetch(\'common/index\');');

    }

    public function head_botton($data){
        $this->str .= '	    $this->headBotton("'.$data['role'].'","'.$data['key'].'","'.$data['name'].'",CONTROLLER_URL . "'.$data['url'].'");'."\r\n";
    }


    public function head_search($data){
        $this->str .= '	    $this->headSearch("'.$data['role'].'","'.$data['key'].'","'.$data['name'].'","'.$data['search_type'].'");'."\r\n";
    }



    public function list_botton($data){
        if ($data['url']=='_edit'){
            $this->edit = '_edit';
        }elseif ($data['url']=='roleEdit'){
            $this->edit = 'roleEdit';
        }
        //依赖
        $str = '""';
        if ($data['key']=='enable'){
            $str = '[\'status\',\'=\',\'2\']';
        }elseif ($data['key']=='disable'){
            $str = '[\'status\',\'=\',\'1\']';
        }
        $this->str .= '	    $this->listBotton("'.$data['role'].'","'.$data['key'].'","'.$data['name'].'",CONTROLLER_URL . "'.$data['url'].'",'.$data['ajax'].',"","","",'.$str.');'."\r\n";
    }

    public function list_title($data){
        if ($data['key']=='status') $data['key'] = 'statusText';
       $this->list_string .= '"'.$data['key'].'"=>"'.$data['name'].'",';
    }







// ---------------------------   index 方法结束  ------------------------------------------------------------------------

    public function addCheck(){
        $this->str .= '    public function check(){
	    $ids = input(\'ids\');
	    admin_return(1,\'勾选并操作成功\');
    }'."\r\n\r\n";
    }

    public function addEdit(){
        $this->str .= '	//$funName 添加和编辑成功后的回调方法,返回 $id,$action , $action =add 是添加，$action = edit 是编辑
    public function _edit($updata = false, $add = false, $noRepeat = false, $funName = false)
    {
//        $updata[\'table\'] = "";//更新的表，默认自动获取
//        $updata[\'key\'] = "";//表的主键，默认自动获取
//        $noRepeat[\'map\'] = "";//新增判断条件
//        $noRepeat[\'name\'] = "";//字段，不可重复的字段
//        $noRepeat[\'tig\'] = "";//提示语
        parent::_edit($updata, $add, $noRepeat, $funName);
    }'."\r\n\r\n";
    }

    public function addRoleEdit(){
        $this->str .= '    //$add_admin = true 新增admin账号。fales  不添加
    public function roleEdit($roleName = false, $add_admin = true)
    {
        parent::roleEdit($roleName, $add_admin);
    }'."\r\n\r\n";
    }

    public function addDel(){
        $this->str .= '    public function _del($tabelName = false, $tableKey = \'id\', $child = false, $delChild = false)
    {
//        $child[\'tableName\'] = \'\';//下级表，如果这个是分类表，使用该分类的表   如:goods 是goods_type的下级
//        $child[\'key\'] = \'\';//外键 如：goods_type_id
//        $child[\'msg\'] = \'该分类下有数据，不能删除\';//提示语
//        $delChild[\'tableName\'] = \'\';///下级表，如果这个是分类表，使用该分类的表，删除该分类的数据 如:goods 是goods_type的下级
//        $delChild[\'key\'] = \'\';//外键 如：goods_type_id
        //$child = [\'tableName\'=>\'shop\',\'key\'=>\'agent_id\',\'msg\'=>\'该分类下有数据，不能删除\'];
        parent::_del($tabelName, $tableKey, $child, $delChild);
    }'."\r\n\r\n";
    }

    public function addRoleDel(){
        $this->str .= '    public function roleDelete($roleName = false, $id = false)
    {
        parent::roleDelete($roleName, $id);
    }'."\r\n\r\n";
    }
}